package cn.xdf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author lscl
 * @version 1.0
 * @intro: @ModelAttribute注解作用与方法上
 */
@Controller
@RequestMapping("/demo08")
public class Demo08Controller_ModelAttribute {
    @ModelAttribute //相当于前置通知
    public void model(Model model, ModelMap modelMap, Map map) {//都是bindmap，内存地址值都是同一个，先执行这个方法
        model.addAttribute("username", "zhangsan");
        modelMap.addAttribute("password", "admin");
        map.put("email","zhangsan@qq.com");
    }

    @RequestMapping("/demo01")
    public void demo01(
            @ModelAttribute("username") String username,// 注意: 形参名和BindingMap中的key一样也要写
            @ModelAttribute("password") String password,
            @ModelAttribute("email") String email,
            HttpServletResponse response
    ) throws Exception {

        response.getWriter().println("username: " + username);
        response.getWriter().println("password: " + password);
        response.getWriter().println("email: " + email);
    }
}
